package com.kzd.btgame.main

import android.app.Activity
import android.os.Bundle
import android.os.Parcelable
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.kzd.btgame.app.SPSConstance
import com.kzd.btgame.main.access.AccessActivity
import kotlinx.parcelize.Parcelize

fun checkLogin(
    bundle: Bundle? = null, clz: Class<out Activity?>,
    action: AccessAction = AccessAction.LOGIN,
) {
    val invoker = LoginCarrier(bundle, clz)
    if (SPSConstance.uid.isNullOrEmpty()) {
        toAccessActivity(action, invoker)
    } else {
        invoker.invoke()
    }
}

fun checkLogin(
    bundle: Bundle? = null, clz: String,
    action: AccessAction = AccessAction.LOGIN,
) {
    val invoker = LoginCarrier(bundle, mClassName = clz)
    if (SPSConstance.uid.isNullOrEmpty()) {
        toAccessActivity(action, invoker)
    } else {
        invoker.invoke()
    }
}

fun checkLogin(clz: Class<out Activity?>) {
    checkLogin(bundle = null, clz = clz)
}

fun checkLogin(clz: String) {
    checkLogin(bundle = null, clz = clz)
}

fun checkLogin(block: () -> Unit) = when {
    SPSConstance.uid.isNullOrEmpty() -> {
        toAccessActivity()
    }
    else -> {
        block.invoke()
    }
}

fun checkLogin(): Boolean {
    return !SPSConstance.uid.isNullOrEmpty()
}

@Parcelize
data class LoginCarrier(
    val mBundle: Bundle? = null,
    val mClass: Class<out Activity>? = null,
    val mClassName: String? = null,
) : Parcelable {
    operator fun invoke() {
        if (mClass != null && mClassName == null) {
            if (mBundle == null) {
                ActivityUtils.startActivity(mClass)
            } else {
                ActivityUtils.startActivity(mBundle, mClass)
            }
        }
        if (mClassName != null && mClass == null) {
            if (mBundle == null) {
                ActivityUtils.startActivity(AppUtils.getAppPackageName(), mClassName)
            } else {
                ActivityUtils.startActivity(mBundle, AppUtils.getAppPackageName(), mClassName)
            }
        }
    }
}

const val KEY_ACCESS_ACTION = "KEY_ACCESS_ACTION"
const val KEY_ACCESS_CARRIER = "KEY_ACCESS_CARRIER"

/**
 * 跳转-登录页面
 *
 * @param action AccessAction
 * */
fun toAccessActivity(action: AccessAction = AccessAction.LOGIN, carrier: LoginCarrier? = null) {
    val bundle = Bundle().apply {
        putSerializable(KEY_ACCESS_ACTION, action)
        if (carrier != null) {
            putParcelable(KEY_ACCESS_CARRIER, carrier)
        }
    }
    ActivityUtils.startActivity(bundle, AccessActivity::class.java)
}

//fun toAccessActivityForResult(
//    activity: Activity,
//    action: AccessAction = AccessAction.LOGIN,
//    carrier: LoginCarrier? = null
//) {
//    val bundle = Bundle().apply {
//        putSerializable(KEY_ACCESS_ACTION, action)
//        if (carrier != null) {
//            putParcelable(KEY_ACCESS_CARRIER, carrier)
//        }
//    }
//    ActivityUtils.startActivityForResult(bundle, activity, AccessActivity::class.java)
//}